<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="商品编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-select v-model="listQuery.status" placeholder="操作状态" clearable class="filter-item" style="width: 130px">
        <el-option v-for="item in statusArr" :key="item.key" :label="item.value" :value="item.key"/>
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">补货下单</el-button>
      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出XLS文件</el-button>
    </div>

    <!--
    <el-row>
      <el-col :span="8" v-for="(o, index) in 2" :key="o" :offset="index > 0 ? 2 : 0">
        <el-card :body-style="{ padding: '0px' }">
          <img src="D:/js/order-admin/src/assets/401_images/hamburger.png" class="image">
          <div style="padding: 14px;">
            <span>好吃的汉堡</span>
            <div class="bottom clearfix">
              <time class="time">552222</time>
              <el-button type="text" class="button">操作按钮</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    -->

    <el-table
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange">
      <el-table-column label="订单编号" sortable="custom" min-width="60px">
        <template slot-scope="scope">
          <span>DD201900008</span>
        </template>
      </el-table-column>
      <el-table-column label="商户名称" width="135px" align="center">
        <template slot-scope="scope">
          <img src="/src/assets/401_images/smallshoes.png" class="link-type" @click="handleUpdate()">
        </template>
      </el-table-column>
      <el-table-column label="商品编号" prop="id" align="center" width="120">
        <template slot-scope="scope">
          <span>XS261H505368</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="120px" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="dialogTableVisible = true">西村明物女鞋</el-button>
        </template>
      </el-table-column>
      <el-table-column label="商品颜色" align="center" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.pageviews">黑色</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
      <el-table-column label="商品尺码" align="center" width="130">
        <template slot-scope="scope">
          <span v-if="scope.row.pageviews">35(2),36(3),37(5)</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
      <el-table-column label="预订单价" align="center" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.pageviews">599</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
      <el-table-column label="下单数量" width="80" align="center">
        <template slot-scope="scope">
          <span>10</span>
        </template>
      </el-table-column>
      <el-table-column label="下单金额" align="center" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.pageviews">5990</span>
          <span v-else>0</span>
        </template>
      </el-table-column>
      <el-table-column label="下单日期" width="100px" align="center">
        <template slot-scope="scope">
          <span>2019-01-28 20:58:36</span>
        </template>
      </el-table-column>
      <el-table-column label="操作状态" class-name="status-col" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">下单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status!='published'" size="mini" type="success" @click="handleModifyStatus(scope.row,'published')">下单
          </el-button>
          <el-button v-if="scope.row.status!='draft'" size="mini" @click="handleModifyStatus(scope.row,'draft')">暂存
          </el-button>
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogTableVisible" title="本次下单情况(货号:W32901892001)" center="boolean" show-close="boolean">
      <el-table :data="gridData" border>
        <el-table-column width="800" label="A店下单预订情况">
          <el-table-column width="110" property="color" label="颜色"/>
          <el-table-column width="50" property="col34" label="34"/>
          <el-table-column width="50" property="col35" label="35"/>
          <el-table-column width="50" property="col36" label="36"/>
          <el-table-column width="50" property="col37" label="37"/>
          <el-table-column width="50" property="col38" label="38"/>
          <el-table-column width="50" property="col39" label="39"/>
          <el-table-column width="50" property="col40" label="40"/>
          <el-table-column width="50" property="col41" label="41"/>
          <el-table-column width="50" property="col42" label="42"/>
          <el-table-column width="50" property="col43" label="43"/>
          <el-table-column width="50" property="col44" label="44"/>
          <el-table-column width="80" property="total" label="合计"/>
        </el-table-column>
      </el-table>
      <el-table :data="gridData" border>
        <el-table-column width="800" label="B店下单预订情况">
          <el-table-column width="110" property="color" label="颜色"/>
          <el-table-column width="50" property="col34" label="34"/>
          <el-table-column width="50" property="col35" label="35"/>
          <el-table-column width="50" property="col36" label="36"/>
          <el-table-column width="50" property="col37" label="37"/>
          <el-table-column width="50" property="col38" label="38"/>
          <el-table-column width="50" property="col39" label="39"/>
          <el-table-column width="50" property="col40" label="40"/>
          <el-table-column width="50" property="col41" label="41"/>
          <el-table-column width="50" property="col42" label="42"/>
          <el-table-column width="50" property="col43" label="43"/>
          <el-table-column width="50" property="col44" label="44"/>
          <el-table-column width="80" property="total" label="合计"/>
        </el-table-column>
      </el-table>
      <el-table :data="gridData" border>
        <el-table-column width="800" label="C店下单预订情况">
          <el-table-column width="110" property="color" label="颜色"/>
          <el-table-column width="50" property="col34" label="34"/>
          <el-table-column width="50" property="col35" label="35"/>
          <el-table-column width="50" property="col36" label="36"/>
          <el-table-column width="50" property="col37" label="37"/>
          <el-table-column width="50" property="col38" label="38"/>
          <el-table-column width="50" property="col39" label="39"/>
          <el-table-column width="50" property="col40" label="40"/>
          <el-table-column width="50" property="col41" label="41"/>
          <el-table-column width="50" property="col42" label="42"/>
          <el-table-column width="50" property="col43" label="43"/>
          <el-table-column width="50" property="col44" label="44"/>
          <el-table-column width="80" property="total" label="合计"/>
        </el-table-column>
      </el-table>
      <el-table :data="gridData" border>
        <el-table-column width="800" label="所有店下单预订总计">
          <el-table-column width="110" property="color" label="颜色"/>
          <el-table-column width="50" property="col34" label="34"/>
          <el-table-column width="50" property="col35" label="35"/>
          <el-table-column width="50" property="col36" label="36"/>
          <el-table-column width="50" property="col37" label="37"/>
          <el-table-column width="50" property="col38" label="38"/>
          <el-table-column width="50" property="col39" label="39"/>
          <el-table-column width="50" property="col40" label="40"/>
          <el-table-column width="50" property="col41" label="41"/>
          <el-table-column width="50" property="col42" label="42"/>
          <el-table-column width="50" property="col43" label="43"/>
          <el-table-column width="50" property="col44" label="44"/>
          <el-table-column width="80" property="total" label="总计"/>
        </el-table-column>
      </el-table>
      <template>
        <el-button align="right" type="info" size="small" @click="handleUpdate(scope.row)">暂存订单</el-button>
        <el-button align="right" type="primary" size="small" @click="handleUpdate(scope.row)">提交下单</el-button>
      </template>
    </el-dialog>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="订单编号" prop="title">
          <el-input name="code" value="DD201900008"/>
        </el-form-item>
        <el-form-item label="商户编号" prop="title">
          <el-input name="code" value="SH093826584"/>
        </el-form-item>
        <el-form-item label="商户名称" prop="title">
          <el-input name="name" value="宁波太晶鞋业商行"/>
        </el-form-item>
        <el-form-item label="商品编号" prop="title">
          <el-input name="code" value="XS261H505368"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input name="name" value="西村明物女鞋"/>
        </el-form-item>
        <el-form-item label="商品颜色" prop="title">
          <el-input name="PayRate" value="黑色"/>
        </el-form-item>
        <el-form-item label="商品尺码" prop="title">
          <el-input name="PayRate" value="35(2),36(3),37(5)"/>
        </el-form-item>
        <el-form-item label="预订单价" prop="title">
          <el-input name="PayRate" value="599"/>
        </el-form-item>
        <el-form-item label="预订数量" prop="title">
          <el-input name="LinkMan" value="10"/>
        </el-form-item>
        <el-form-item label="预订金额" prop="title">
          <el-input name="LinkTelephone" value="5990"/>
        </el-form-item>
        <el-form-item label="下单描述" prop="title">
          <el-input :autosize="{ minRows: 2, maxRows: 4}" value="希望能够尽快在30天内发货" type="textarea"/>
        </el-form-item>
        <el-form-item label="下单日期" prop="timestamp">
          <el-date-picker value="2019-01-28 20:58:36" type="datetime" placeholder="Please pick a date"/>
        </el-form-item>
        <el-form-item label="操作状态" prop="title">
          <el-select v-model="temp.status" class="filter-item" placeholder="Please select">
            <el-option v-for="item in statusArr" :key="item.key" :label="item.value" :value="item.key"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/article'
import waves from '@/directive/waves' // Waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

const statusNameMap = {
  published: '下单',
  draft: '暂存',
  deleted: '删除'
}

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    statusNameFilter(status) {
      return statusNameMap[status]
    }
  },
  data() {
    return {
      gridData: [{
        color: '黑色-01',
        col34: '1',
        col35: '1',
        col36: '1',
        col37: '1',
        col38: '1',
        col39: '1',
        col40: '1',
        col41: '1',
        col42: '1',
        col43: '1',
        col44: '1',
        total: '11'
      }, {
        color: '灰色-05',
        col34: '2',
        col35: '2',
        col36: '2',
        col37: '2',
        col38: '2',
        col39: '2',
        col40: '2',
        col41: '2',
        col42: '2',
        col43: '2',
        col44: '2',
        total: '22'
      }, {
        color: '白色-12',
        col34: '3',
        col35: '3',
        col36: '3',
        col37: '3',
        col38: '3',
        col39: '3',
        col40: '3',
        col41: '3',
        col42: '3',
        col43: '3',
        col44: '3',
        total: '33'
      }, {
        color: '合计',
        col34: '6',
        col35: '6',
        col36: '6',
        col37: '6',
        col38: '6',
        col39: '6',
        col40: '6',
        col41: '6',
        col42: '6',
        col43: '6',
        col44: '6',
        total: '66'
      }],
      dialogTableVisible: false,
      tableLabelWidth: '120px',
      statusMap: {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      },
      statusArr: [
        {
          key: 'published',
          value: '下单'
        },
        {
          key: 'draft',
          value: '暂存'
        },
        {
          key: 'deleted',
          value: '删除'
        }
      ],
      statusNameMap,
      tableKey: 0,
      list: [],
      totalList: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        title: undefined,
        status: undefined,
        sort: '+id'
      },
      showReviewer: false,
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (this.totalList.length == 0) {
        this.total = 200
        for (let i = 0; i < this.total; i++) {
          this.totalList.push({
            id: i + 1,
            author: '鲁迅',
            title: '标题' + (i + 1),
            timestamp: 1548680325680,
            status: 'published',
            pageviews: 1,
            reviewer: '主编'
          })
        }
      } else {
        this.totalList = this.totalList.filter(e => {
          let flag = true
          if (this.listQuery.status) {
            flag = e.status == this.listQuery.status
          }
          if (this.listQuery.title) {
            flag = e.title == this.listQuery.title
          }
          return flag
        })
      }
      this.list = this.totalList.slice((this.listQuery.page - 1) * this.listQuery.limit, this.listQuery.page * this.listQuery.limit)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          this.temp.author = 'vue-element-admin'
          createArticle(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          updateArticle(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$notify({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
